package com.cn.por.biz.user.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.cn.por.biz.user.dao.UserDao;
import com.cn.por.biz.user.dto.ProUserDetails;
import com.cn.por.biz.user.dto.UserDO;
import com.cn.por.biz.user.role.dto.UserRole;
import com.cn.por.biz.user.service.UserService;
/**
 * 用户接口
 * @author wen
 *
 */

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	/**
	 * 获取所有用户信息
	 */
	@Override
	public List<ProUserDetails> getUserList() {
		return userDao.getUserList();
	}
	
	public int addUser(ProUserDetails user) {
		/**
		 * 判断用户名是否已经存在
		 */
		if(null!=userDao.getUserByName(user.getUsername())) {
			return 0;
		}
		UserDO userDO = new UserDO();
		userDO.setUsername(user.getUsername());
		if(null!=user.getAccountNonExpired()) {
			userDO.setAccountNonExpired(user.getAccountNonExpired() ? 1 : 0);
		}
		if(null!=user.getAccountNonLocked() ) {
			userDO.setAccountNonLocked(user.getAccountNonLocked() ? 1 : 0);
		}
		if(null!=user.getCredentialsNonExpired()) {
			userDO.setCredentialsNonExpired(user.getCredentialsNonExpired() ? 1 :0);
		}
		if(null!=user.getEnabled() ) {
			userDO.setEnabled(user.getEnabled() ? 1 : 0);
		}
		userDO.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
		return userDao.addUser(userDO);
	}

	@Override
	public int addUserRole(UserRole userRole) {
		List<UserRole> userRoleList = userDao.getUserRole(userRole);
		if(null!=userRoleList && userRoleList.size()>0) {
			return 0;
		}
		return userDao.addUserRole(userRole);
	}

}
